package consts

import (
	"github.com/cloudwego/hertz-examples/bizdemo/hertz_gorm/biz/model"
	"github.com/cloudwego/hertz/pkg/app"
)

func GetUserInfoFromCtx(ctx *app.RequestContext) (*model.User, bool) {
	value, exists := ctx.Get("user_info")
	if !exists {
		return nil, false
	}
	if _, ok := value.(*model.User); !ok {
		return nil, false
	}
	return value.(*model.User), true
}

func SetUserInfoToCtx(ctx *app.RequestContext, user *model.User) {
	ctx.Set("user_info", user)
}

func SetTokenToCtx(ctx *app.RequestContext, token string) {
	ctx.Set("token", token)
}

func GetTokenFromCtx(ctx *app.RequestContext) (string, bool) {
	token, exists := ctx.Get("token")
	if !exists {
		return "", false
	}
	if _, ok := token.(*model.User); !ok {
		return "", false
	}
	return token.(string), true
}
